package Homework.jdbcWork2;

import jdbc.DButil;

import java.sql.*;
import java.util.Scanner;

/**
 * @author shkstart
 * @creat 2022-05-14:39
 */
public class returnPrimarykey {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Connection con =null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            con = DButil.getCon();
            System.out.println("输入姓名");
            //设置返回主键
            ps=con.prepareStatement("insert into js (s_name)values (?)", Statement.RETURN_GENERATED_KEYS);
            ps.setString(1,sc.next());
            int i = ps.executeUpdate();
            System.out.println(i>0?"插入数据成功":"插入数据失败");
            //获取返回的主键
            rs = ps.getGeneratedKeys();
            while (rs.next()){
                System.out.println("id:"+rs.getInt(1));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DButil.close(rs,ps,con);
        }
    }
}
